package cn.com.shinho.hg.gate.filter;

import cn.com.shinho.hg.core.common.constants.HGConstants;
import cn.com.shinho.hg.core.util.IdGen;
import cn.com.shinho.hg.gate.constants.ZuulConstants;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import javax.servlet.http.HttpServletResponse;

/**
 * @program: hg-server-parent
 * @description: ${description}
 * @author: Mr.Ye
 * @create: 2018-08-10
 **/
public class RequestNoGenerateFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return ZuulConstants.REQUEST_NO_GENERATE_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletResponse response = currentContext.getResponse();

        String requestNo = IdGen.getNextId();

        currentContext.addZuulRequestHeader(HGConstants.REQUEST_NO_HEADER_NAME, requestNo);

        response.addHeader(HGConstants.REQUEST_NO_HEADER_NAME, requestNo);

        return null;
    }
}
